// SGJ Apps Bible Input Method (BibleIM).  A collection
// of Java InputMethods enabling the easy typing of Biblical scripts.
// Copyright (C) 2007-2008 Steffen G. Jenkins  

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

// This file is based on the following source file.
/*
Brahmi Kannada Input Method
http://brahmi.sourceforge.net
Copyright (C) 2002  Harsha P Ravnikar <harsha_pr@users.sourcefourge.net>
This file is a part of Brahmi Kannada Input Method
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package uk.org.sgj.BibleIM;

import java.awt.event.KeyEvent;
import java.awt.font.TextHitInfo;
import java.awt.im.spi.InputMethodContext;
import java.text.AttributedString;
import java.awt.event.InputMethodEvent;

public class InputMethodExec implements uk.org.sgj.BibleIM.CharacterMapper
{
    private int committedChars;
    private int totalTypedChars;
    private char text[];
    protected char KEYBOARD_MAP[];
    private InputMethodContext inputMethodContext;
    private boolean ukLayout;
    
    public InputMethodExec(char kbm[], boolean ukL)
    {
        text = new char[1];
        text[0] = '\uFFFF';
        committedChars = 0;
        totalTypedChars = 0;
        KEYBOARD_MAP = kbm;
        ukLayout = ukL;
    }
    
    public char getMappedChar(char ch)
    {
        char c =  convertUKtoUSCharCode(ch);
        if (c <= KEYBOARD_MAP.length)
        {
            return KEYBOARD_MAP[c];
        }
        else
        {
            return c;
        }
    }
    
    public char getUKUSChar(char ch)
    {
        char c =  convertUKtoUSCharCode(ch);
        return c;
    }
    
    private char convertUKtoUSCharCode(char ukChar)
    {
        char usChar = ukChar;
        
        if (!ukLayout)
        {
            switch(ukChar)
            {
                case 34:
                {
                    usChar = 64;
                    break;
                }
                case 163:
                {
                    usChar = 35;
                    break;
                }
                case 64:
                {
                    usChar = 34;
                    break;
                }
                case 126:
                {
                    usChar = 124;
                    break;
                }
                case 35:
                {
                    usChar = 92;
                    break;
                }
                case 172:
                {
                    usChar = 126;
                    break;
                }
            }
        }
        
        return(usChar);
    }
    
    void setInputMethodContext(InputMethodContext imContext)
    {
        inputMethodContext = imContext;
    }
    
    private char c1=0;
    private char c2=0;
    
    void processKeyEvent(KeyEvent keyevent)
    {
        char c = keyevent.getKeyChar();
        c2 = c1;
        c1 = getMappedChar(c);
        
        if (c1 == '\b')
        {
            if (totalTypedChars > 0)
            {
                totalTypedChars = committedChars = 0;
            }
            else
            {
                return;
            }
        }
        else
        {
            text[totalTypedChars++] = (char)c1;
            committedChars = totalTypedChars;
        }
        
        TextHitInfo texthitinfo = null;
        TextHitInfo texthitinfo1 = null;
        
        String stext = new String(text);
        
        //KEEP THIS FOR DEBUGGING.
//        String infoText = " ("+Integer.toString(c)+") ";
//        stext = infoText.concat(stext);
        
        AttributedString attributedString = new AttributedString(stext);
        inputMethodContext.dispatchInputMethodEvent(InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
                attributedString.getIterator(),
                committedChars,
                texthitinfo,
                texthitinfo1);
        
        if (totalTypedChars == 0)
        {
            text[0] = '\uFFFF';
        }
        else
        {
            text[0] = text[totalTypedChars - 1];
        }
        
        totalTypedChars -= committedChars;
        committedChars = 0;
        keyevent.consume();
    }
    
    void commit()
    {
        if (totalTypedChars != 0)
        {
            String stext = new String(text);
            AttributedString attributedString = new AttributedString(stext);
            inputMethodContext.dispatchInputMethodEvent(InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
                    attributedString.getIterator(),
                    totalTypedChars,
                    null,
                    null);
            totalTypedChars = committedChars = 0;
            text[0] = '\uFFFF';
        }
    }
}